package logiclib

import (
	"MqttCloudProtocolServer/publiclib/datamodel"
	"MqttCloudProtocolServer/publiclib/define"
	"encoding/json"
	"os"

	"github.com/wonderivan/logger"
)

// ConfigFileBase 提供基本函数定义。需要集成实现
type ConfigFileBase struct {
}

// LoadProcessCfg fun
func (_this *ConfigFileBase) LoadProcessCfg() []datamodel.TitleValueStruct {
	//读取服务列表配置
	if buffer, err := os.ReadFile(define.ConfigPath + "service.json"); err != nil {
		logger.Fatal("服务列表读取错误" + err.Error())
	} else {
		confs := []datamodel.TitleValueStruct{}
		if err = json.Unmarshal(buffer, &confs); err != nil {
			logger.Fatal("服务列表读取错误")
		}
		return confs
	}
	return nil
}

// LoadPortCfg fun
func (_this *ConfigFileBase) LoadPortCfg() []datamodel.TitleValueStruct {
	//读取服务列表配置
	if buffer, err := os.ReadFile(define.ConfigPath + "ports.json"); err != nil {
		logger.Error("端口列表读取错误" + err.Error())
	} else {
		confs := []datamodel.TitleValueStruct{}
		if err = json.Unmarshal(buffer, &confs); err != nil {
			logger.Error("端口列表读取错误")
		}
		return confs
	}
	return nil
}

// GetValueFromKey fun
func (_this *ConfigFileBase) GetValueFromKey(buffer []datamodel.TitleValueStruct, key string) (string, bool) {
	if buffer == nil {
		return "", false
	}
	for _, item := range buffer {
		if item.Title == key {
			return item.Value, true
		}
	}
	return "", false
}
